//
//  RetweetStatusView.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
//转发微博
class RetweetStatusView: UIView {

    
    // 当前控件的底部约束：需要根据是否有配图去动态更新
    var bottomCons: Constraint?
    
    var statusViewModel : Homecellviewmdoel?{
        
        didSet{
            // 设置转发微博内容
            //            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            contentLabel.text = statusViewModel?.retweetText
            // 0. 卸载掉之前的底部约束
            bottomCons?.uninstall()
            // 设置配图数据
            // - 如果有配图
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
//                // 判断如果是1张图片，设置itemSize为配图视图的大小
//                if pic_urls.count == 1 {
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statusViewModel!.retweetPictureViewSize
//                }else{
//                    // 否则设置成正方形 HMStatusPictureItemWH
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
//                }
//            // - 1. 给配图视图设置数据（以及大小宽高）
//            pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                make.size.equalTo(statusViewModel!.retweetPictureViewSize)
//            })
//            // 给配图视图赋值
////            pictureView.pic_urls = pic_urls
//
//            // 给配图视图设置数据以及大小宽高
//            pictureView.dataInfo = (pic_urls, statusViewModel!.retweetPictureViewSize)
//            
                pictureView.dataInfo = (pic_urls, statusViewModel!.retweetPictureViewSize)
                
            // - 2. 将配图显示出来
            pictureView.hidden = false
            // - 3. 以转发微博视图为例：转发微博视图的底部等于配图视图的底部
            self.snp_makeConstraints(closure: { (make) -> Void in
                bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
            })
            
        }else{// - 如果没有配图
        // - 1. 隐藏
        pictureView.hidden = true
        // - 2. 以转发微博视图为例：转发微博视图的底部等于文字label的底部
        self.snp_makeConstraints(closure: { (make) -> Void in
        bottomCons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
        })
        }
    }
}

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    显示内容
    private func setupUI(){
//        背景色
        backgroundColor = UIColor(white: 180/255, alpha: 1)
//        添加到转发内容到view上
        addSubview(contentLabel)
        addSubview(pictureView)
//        约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            // 为了让大家看到效果。宽高暂时写死
//            make.size.equalTo(CGSizeMake(100, 100))
        }

        self.snp_makeConstraints { (make) -> Void in
          bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(HMStatusCellMargin).constraint
        }
    }


    
    // MARK: - 懒加载控件
    
    // 转发微博显示内容的lable 和frame
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
    
    /// 配图视图
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
